#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
    pid_t id = fork();

    if (id > 0)
    {
        printf("我是【父进程】。pid: %d; ppid: %d\n", getpid(), getppid());
        printf("【父进程】已退出!!!\n");
        sleep(2);
    }
    else if (0 == id)
    {
        pid_t prevParent = getppid();
        printf("我是【子进程】。pid: %d; ppid: %d\n", getpid(), getppid());
        sleep(5);
        printf("我现在是【孤儿进程】。我的父进程从 %d 变为了 %d\n", prevParent, getppid());
        sleep(2);
    }
    else
    {
        perror("fork fail");
    }
    return 0;
}






